package com.qiwx.pigeon.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.activity.common.SelectDateActivity;
import com.qiwx.pigeon.widget.SelectDatePopWindow;

import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_END;
import static com.qiwx.pigeon.activity.common.SelectDateActivity.REQUEST_CODE_START;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-25 19:28
 **/
public abstract class BaseMVPFragment <V extends IBaseView, T extends BasePresenter<V>> extends BaseFragment {
    protected T mPresenter;
    protected SelectDatePopWindow popWindow;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void initAfterOnCreate() {
        mPresenter = createPresenter();

    }
    //刷新数据
    public void onReFreshData(){


    }
    public abstract T createPresenter();

    protected void onSelectDate(int type,String content){

    }


    public void selectStartDate(){
        selectDate("2018-01",0);
    }
    //选择日期
    public void selectDate(String date, int type) {
        date = date.replace("年", "")
                .replace("日", "");
        String[] arr = date.split("-");
        if (popWindow == null) {
            popWindow = new SelectDatePopWindow(getActivity());
            popWindow.setListener((t, content) -> {

                onSelectDate(t,content);

            });
        }
        if (type == 0) {
            popWindow.setTitle("开始日期");
        } else {
            popWindow.setTitle("结束日期");
        }
        popWindow.initData(type, Integer.valueOf(arr[0]), Integer.valueOf(arr[1]));

        //这种方式无论有虚拟按键还是没有都可完全显示，因为它显示的在整个父布局中
        popWindow.showAtLocation(getActivity().findViewById(R.id.lin_main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    //选择有效期
    public void selectStartValiDate() {
        Intent intent = new Intent(getActivity(), SelectDateActivity.class);
//        intent.putExtra("term", 3);
        intent.putExtra("MinMonth", DateTimeUtils.getMonth4String("1990-01"));
        intent.putExtra("Minyear", DateTimeUtils.getYear4String("1990-01"));

        intent.putExtra(Constant.KEY_TITLE, "开始日期");
        startActivityForResult(intent, REQUEST_CODE_START);
    }

    public void selectEndValiDate(String startDate) {
        if (startDate == null || startDate.equals("")) {
            MyToast.showShort("先选择开始日期");
            return;
        }
        startDate = startDate.replace("年", "")
                .replace("日", "");

        String[] arr = startDate.split("-");
        Intent intent = new Intent(getActivity(), SelectDateActivity.class);
//        intent.putExtra("term", 3);
        intent.putExtra("Minyear", arr[0]);
        intent.putExtra("MinMonth", arr[1]);
        intent.putExtra(Constant.KEY_TITLE, "结束日期");
        startActivityForResult(intent, REQUEST_CODE_END);
    }

    @Override
    public void onDestroyView() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroyView();
    }

}
